//
//  PriceSettingViewController.m
//  OutesIOS
//
//  Created by norman on 2019/2/28.
//  Copyright © 2019 norman. All rights reserved.
//

#import "PriceSettingViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "EditorTableViewCell.h"
#import "HttpTool.h"

@interface PriceSettingViewController ()

@property (nonatomic,strong) TPKeyboardAvoidingTableView *tableView;
@property (nonatomic,strong) NSMutableDictionary *settingData;

@end

@implementation PriceSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"价格设置";
    [self setupViews];
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self updateSetting];
}

- (void)setupViews{
    //列表
    self.tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStylePlain];
    self.tableView.backgroundColor = CM_TABLE_BG_COLOR;
    [self.tableView registerClass:[EditorTableViewCell class] forCellReuseIdentifier:@"edit"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //去掉多余的分割线
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadData {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:[AccountManager manager].token forKey:@"token"];
    [HttpTool get:HTTP_GET_SETTING_INFO params:param success:^(NSDictionary *responseObj) {
        [self.tableView.mj_header endRefreshing];
        self.settingData = [responseObj mutableCopy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)updateSetting {
    //参数
    if (self.settingData == nil) {
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[self.settingData objectForKey:@"e_price"] forKey:@"e_price"];
    [params setValue:[self.settingData objectForKey:@"w_price"] forKey:@"w_price"];
    [params setValue:[self.settingData objectForKey:@"a_price"] forKey:@"a_price"];
    [params setValue:[self.settingData objectForKey:@"c_price"] forKey:@"c_price"];
    [params setValue:[self.settingData objectForKey:@"o_price"] forKey:@"o_price"];
    [params setValue:[self.settingData objectForKey:@"b_price"] forKey:@"b_price"];
    [params setValue:[self.settingData objectForKey:@"s_price"] forKey:@"s_price"];
    [params setValue:[self.settingData objectForKey:@"h_price"] forKey:@"h_price"];
    [HttpClient postWithUrl:HTTP_POST_SETTING_PRICE token:[AccountManager manager].token paramDic:params resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
    } failure:^(NSError *error) {
    }];
}

#pragma mark - tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 8;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"edit"];
    [cell setKeyboardType:UIKeyboardTypeDecimalPad];
    if (indexPath.row == 0) {
        [cell setTitleStr:@"电价" andColor:nil andHintStr:@"请输入电价" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"元/度"];
        if ([[self.settingData objectForKey:@"e_price"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"e_price"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"e_price"];
        };
    } else if (indexPath.row == 1) {
        [cell setTitleStr:@"水价" andColor:nil andHintStr:@"请输入水价" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"元/m³"];
        if ([[self.settingData objectForKey:@"w_price"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"w_price"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"w_price"];
        };
    } else if (indexPath.row == 2) {
        [cell setTitleStr:@"天然气价" andColor:nil andHintStr:@"请输入天然气价" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"元/m³"];
        if ([[self.settingData objectForKey:@"a_price"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"a_price"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"a_price"];
        };
    } else if (indexPath.row == 3) {
        [cell setTitleStr:@"煤价" andColor:nil andHintStr:@"请输入煤价" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"元/kg"];
        if ([[self.settingData objectForKey:@"c_price"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"c_price"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"c_price"];
        };
    } else if (indexPath.row == 4) {
        [cell setTitleStr:@"重油价" andColor:nil andHintStr:@"请输入重油价" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"元/kg"];
        if ([[self.settingData objectForKey:@"o_price"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"o_price"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"o_price"];
        };
    } else if (indexPath.row == 5) {
        [cell setTitleStr:@"生物质价" andColor:nil andHintStr:@"请输入生物质价" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"元/kg"];
        if ([[self.settingData objectForKey:@"b_price"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"b_price"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"b_price"];
        };
    } else if (indexPath.row == 6) {
        [cell setTitleStr:@"蒸汽价" andColor:nil andHintStr:@"请输入蒸汽价" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"元/m³"];
        if ([[self.settingData objectForKey:@"s_price"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"s_price"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"s_price"];
        };
    } else {
        [cell setTitleStr:@"市政供暖价" andColor:nil andHintStr:@"请输入市政供暖价" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"元/㎡"];
        if ([[self.settingData objectForKey:@"h_price"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"h_price"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"h_price"];
        };
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell1";
    EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[EditorTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return [cell getHeight];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, DEFAULT_SECTION_HEIGHT)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return DEFAULT_SECTION_HEIGHT;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
